package com.jk.vehicle.mini.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.jk.vehicle.core.utils.LocationUtils;
import com.jk.vehicle.mini.dao.NetworkMapper;
import com.jk.vehicle.mini.entity.Network;
import com.jk.vehicle.mini.service.NetworkService;
import com.jk.vehicle.core.service.impl.AbstractService;
import com.jk.vehicle.mini.util.QueryUtil;
import com.jk.vehicle.mini.util.SequenceUtils;
import com.jk.vehicle.mini.vo.NetworkSelectListVo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.mapperhelper.EntityHelper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Date;

/**
 * This is Description for NetworkServiceImpl
 *
 * @author Administrator
 * @date 2021/06/25
 */
@Service
public class NetworkServiceImpl extends AbstractService<Network> implements NetworkService {
    /**
	 * 注入NetworkMapper
	 */
    @Resource
    private NetworkMapper bizNetworkMapper;

    /**
    * 根据查询条件查询单表数据
    * @param paramMap 查询条件
    * @return 查询结果
    */
    public List<Network> queryByParams(Map<String, Object> paramMap){
        Condition condition = new Condition(Network.class);
        Example.Criteria criteria = condition.createCriteria();
        Map<String, EntityColumn> propertyMap = EntityHelper.getEntityTable(Network.class).getPropertyMap();
        QueryUtil.spliceQueryParam(criteria, paramMap,  propertyMap, condition);
        return bizNetworkMapper.selectByCondition(condition);
    }

    /**
     * 根据条件查询网点列表
     * @param corpId 厂家id
     * @return
     */
    @Override
    @DS("#last")
    public List<NetworkSelectListVo> list(String lng, String lat, Double distance, Integer networkType,
                                          Long corpId, String dsName) {
        List<NetworkSelectListVo> resultList = new ArrayList<>();
        List<NetworkSelectListVo> listAdminVoList = bizNetworkMapper.selectNetworkList(networkType, corpId);
        listAdminVoList.forEach(networkSelectListVo -> {
            String[] result = networkSelectListVo.getCenter().split(",");
            Double distanceValue = LocationUtils.getDistance(lng, lat, result[0], result[1]);
            if (listAdminVoList.size() <= 20) {
                if (distanceValue <= distance) {
                    networkSelectListVo.setDistance(distanceValue);
                    resultList.add(networkSelectListVo);
                }
            }
        });
        return resultList;
    }

    @Override
    @DS("#last")
    public void save(Network bizNetwork, String dsName) {
        bizNetwork.setId(SequenceUtils.nextId());
        bizNetwork.setCreateDate(new Date());
        super.save(bizNetwork, dsName);
    }

    @Override
    @DS("#last")
    public void update(Network bizNetwork, String dsName){
        bizNetwork.setUpdateDate(new Date());
        super.update(bizNetwork, dsName);
    }
}
